using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

using SBPweb.Localization;
using SBPweb.Contracts;
using SBPweb.Contracts.Options;
using SBPweb.Modules.Modularity;
using SBPweb.Contracts.Implementation;

namespace SBPweb.Modules.Options.Controls
{
	public partial class CommonTabPage : UserControl
	{
		
		#region Private members

		private List<CultureInfo> mCultures;
		private bool mIsInLoading;
		private IOptionsService mOptionsService;

		#endregion

		#region Constructor logic

		public CommonTabPage(IOptionsService optionsService)
		{
			InitializeComponent();
			mOptionsService = optionsService;
		}

		#endregion

		#region Public properties

		public List<CultureInfo> Cultures
		{
			get
			{
				return mCultures;
			}
			set
			{
				mIsInLoading = true;
				mCultures = value;
                cbCulture.ValueMember = "Value";
                cbCulture.DisplayMember = "Key";
                cbCulture.BeginUpdate();
				cbCulture.Items.Clear();
				if (value != null)
				{
					foreach (CultureInfo ci in value)
					{
						cbCulture.Items.Add(GetKVPForCulture(ci));
					}
				}
				cbCulture.EndUpdate();
				if (mCultures.Contains(System.Threading.Thread.CurrentThread.CurrentUICulture))
				{
					SelectedCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
				}
				mIsInLoading = false;
			}
		}

		public CultureInfo SelectedCulture
		{
			get
			{
				return GetSelectedCultureOfComboBox();
			}
			set
			{
				if (mCultures.Contains(value))
				{
					cbCulture.SelectedItem = GetKVPForCulture(value);
				}
			}
		}

		#endregion

		#region UI event handlers

		private void cbCulture_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!mIsInLoading && mOptionsService!= null)
			{
				mOptionsService.IsModified = true;
			}
		}

		#endregion

		#region Private methods

		private CultureInfo GetSelectedCultureOfComboBox()
        {
			if (cbCulture.SelectedItem != null)
			{
				return ((KeyValuePair<string, CultureInfo>)(cbCulture.SelectedItem)).Value;
			}
			else
			{
				return null;
			}
        }

        private KeyValuePair<string, CultureInfo> GetKVPForCulture(CultureInfo ci)
        {
            return new KeyValuePair<string, CultureInfo>(string.Format("[{0}] - {1}", ci.Name, ci.NativeName), ci);
		}

		#endregion

		#region Persistency

		internal void SavePersistence(PersistencyDictionary state)
		{
			if (cbCulture.SelectedItem != null)
			{
				state.SetString("SelectedCulture", ((KeyValuePair<string, CultureInfo>)(cbCulture.SelectedItem)).Value.ToString());
			}
		}

		internal void LoadPersistence(PersistencyDictionary state)
		{
			string culture = state.GetString("SelectedCulture", string.Empty);
			int selectedCulture = cbCulture.SelectedIndex;
			if (!string.IsNullOrEmpty(culture))
			{
				foreach (KeyValuePair<string, CultureInfo> item in cbCulture.Items)
				{
					if (item.Value.ToString() == culture)
					{
						mIsInLoading = true;
						cbCulture.SelectedItem = item;
						System.Threading.Thread.CurrentThread.CurrentUICulture = item.Value as CultureInfo;
						mIsInLoading = false;
						break;
					}
				}
			}
		}

		#endregion

	}
}
